package com.smforj.ssm.modules.lib.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.smforj.ssm.frame.core.enums.JDataSource;
import com.smforj.ssm.modules.eslib.dao.CirculationDao;
import com.smforj.ssm.modules.eslib.dao.CirculationLogDao;
import com.smforj.ssm.modules.eslib.domain.Circulation;
import com.smforj.ssm.modules.eslib.domain.CirculationLog;
import com.smforj.ssm.modules.eslib.domain.vo.LibBookVo;
import com.smforj.ssm.modules.lib.domain.CheckInResponse;
import com.smforj.ssm.modules.lib.domain.CheckOutResponse;
import com.smforj.ssm.modules.sys.service.CirculationService;
import com.smforj.ssm.modules.sys.util.FatherToChildUtils;
import com.smforj.ssm.util.DateFormat;

@Service
@JDataSource("sdefault")
public class CirculationServiceImpl implements
		CirculationService {
	private Logger log = Logger.getLogger(CirculationServiceImpl.class); 
	
	@Autowired 
	private CirculationDao circulationDao;
	@Autowired 
	private CirculationLogDao circulationLogDao;

	@Override
	public Circulation getCirculationBytsm(String tsm) { 
		return circulationDao.getCirculationBytsm(tsm);
	}

	@Override
	public List<Circulation> getCirculations(String jsrbh) { 
		return circulationDao.getCirculations(jsrbh);
	}

	@Override
	public int insertCirculation(Circulation cir) { 
		return circulationDao.insertCirculation(cir);
	}

	@Override
	public int deleteCirculation(Circulation cir) { 
		return circulationDao.deleteCirculation(cir);
	}

	@Override
	public int insertCirculationLog(CirculationLog cirLog) {  
		int iRes = deleteCirculation(cirLog); 
		return iRes > 0 ? circulationLogDao.insertCirculationLog(cirLog) : 0;
	}

	@Override
	public CheckOutResponse checkOut(LibBookVo book,String readerId) { 
		CheckOutResponse cor = new CheckOutResponse(); 
		cor.setRfid(book.getId());
		cor.setBookId(book.getId());
		cor.setBookName(book.getZtm());
		cor.setOccurDate(DateFormat.dateToString(DateFormat.FORMATDATE));
		try{
			//判断是否已经借阅
			Circulation cir = circulationDao.getCirculationBytsm(book.getTstm());
			//未借阅
			if(cir == null)
			{
				cir = getCirculation(book,readerId);
				int iRes = circulationDao.insertCirculation(cir); 
				if(iRes > 0)
				{
					cor.setStatus(1);
					cor.setMessage("借阅成功"); 
					cor.setNeedReturnDate(cir.getYhrq());
					cor.setOccurDate(cir.getJsrq());
				}else
				{
					cor.setStatus(0);
					cor.setMessage("借阅失败");
				}
				
			} 
			else
			{
				cor.setNeedReturnDate(cir.getYhrq());
				cor.setOccurDate(cir.getJsrq());
				cor.setStatus(0);
				cor.setMessage("图书已经借阅");
			}
				
		}catch(Exception ex)
		{
			log.error("checkOut",ex);
			cor.setStatus(0);
			cor.setMessage("借阅异常");
		}
		return cor;
	} 
	
	
	private Circulation getCirculation(LibBookVo book,String readerId)
	{
		Date date = new Date();
		Circulation cir = new Circulation();
		cir.setTsm(book.getTstm());
		cir.setZch(book.getZch());
		cir.setFlh(book.getFlh());
		cir.setSm(book.getZtm());
		cir.setJsrq(DateFormat.dateToString(date, DateFormat.FORMATDATE));
		cir.setYhrq(DateFormat.dateToString(book.getYhrq(), DateFormat.FORMATDATE));
		cir.setGcd(book.getGcd());
		cir.setHsrq(null);
		cir.setXj(0);
		cir.setJsrbh(readerId);
		cir.setCzy("Terminal");
		cir.setZchfz(book.getZchfz());
		cir.setLx(""); 
		return cir;
	}
	/***
	 * 获取日期
	 * @param date 添加或减少天数
	 * @return
	 * @date 2017-7-5 下午4:23:21
	 */
	public Date getDate(Date d,int date)
	{ 
		Calendar  calendar   =   new   GregorianCalendar(); 
	    calendar.setTime(d); 
	    calendar.add(Calendar.DATE,10);//把日期往后增加一天.整数往后推,负数往前移动  
	    return calendar.getTime();
	}

	@Override
	public CheckInResponse checkIn(LibBookVo book) {
		CheckInResponse response = new CheckInResponse();
		response.setRfid(book.getId());
		response.setBookId(book.getId());
		response.setBookName(book.getZtm());
		response.setOccurDate(DateFormat.dateToString(DateFormat.FORMATDATE));
		try{
			Circulation cir = getCirculationBytsm(book.getTstm());
			if(cir != null)
			{
				int iRes = circulationDao.deleteCirculation(cir); 
				if(iRes > 0)
				{
					CirculationLog cirLog = new CirculationLog();
					FatherToChildUtils.fatherToChild(cir, cirLog);
					cirLog.setHsrq(DateFormat.dateToString(DateFormat.FORMATDATE));
					cirLog.setFj(0.00);
					circulationLogDao.insertCirculationLog(cirLog);
				}
				response.setStatus(1);
				response.setMessage("还书成功"); 
			}else
			{
				response.setStatus(0);
				response.setMessage("还书失败"); 
			}
		}catch(Exception t)
		{
			log.error("还书失败", t);
			response.setStatus(0);
			response.setMessage("还书失败"); 
		}
		return response;
	}
	
	public String getRenewDate(String dateString,int date)
	{ 
		return DateFormat.dateToString(getDate(DateFormat.stringToDate(dateString),date),DateFormat.FORMATDATE);
	}

	@Override
	public CheckOutResponse reNew(LibBookVo book, String readerId) { 
		CheckOutResponse cor = new CheckOutResponse(); 
		cor.setRfid(book.getId());
		cor.setBookId(book.getId());
		cor.setBookName(book.getZtm());
		cor.setNeedReturnDate(DateFormat.dateToString(DateFormat.FORMATDATE));
		try{ 
			Circulation cir = circulationDao.getCirculation(book.getTstm(), readerId); 
			if(cir != null)
			{
				cir.setXj(cir.getXj()+1);
				//String date = cir.getYhrq();
				//System.out.println("------------"+date);
				//System.out.println("------------"+getRenewDate(date, 10));
				cir.setYhrq(DateFormat.dateToString(book.getYhrq(),DateFormat.FORMATDATE));
				cir.setJsrq(DateFormat.dateToString(DateFormat.FORMATDATE));
				int iRes = circulationDao.updateCirculation(cir); 
				if(iRes > 0)
				{
					cor.setStatus(1);
					cor.setMessage("续借成功"); 
					cor.setNeedReturnDate(cir.getYhrq());
					cor.setOccurDate(cir.getJsrq());
				}else
				{
					cor.setStatus(0);
					cor.setMessage("续借失败");
				}
			}else
			{
				cor.setStatus(0);
				cor.setMessage("图书未借阅");
			}
				
		}catch(Exception ex)
		{
			log.error("checkOut",ex);
			cor.setStatus(0);
			cor.setMessage("续借异常");
		}
		return cor;
	} 

	@Override
	public Circulation getCirculationByRfid(String rfid) { 
		return circulationDao.getCirculationByRfid(rfid);
	}

	@Override
	public int payFee(String jsrbh, String barcode, Double fj) { 
		return circulationLogDao.updateCirculationLog(jsrbh, barcode, fj);
	}

	@Override
	public List<CirculationLog> selectCirculationLogByJsrbh(String jsrbh) { 
		return circulationLogDao.selectCirculationLogByJsrbh(jsrbh);
	}
	
	
}
